cd "C:\Users\bhighton\Desktop\Dropbox\_My Documents_\Collaborations\asymmetry\analysis\senate\lsq replication materials"
set more off

clear
use "C:\Users\bhighton\Desktop\Dropbox\_My Documents_\Data\CCES 2014\CCES14_Common_OUTPUT_Feb2015.dta" 


* converting variable names to lowercase *
rename _all,lower

rename stateabbr abbrev
drop region
merge m:1 abbrev using "StateCodesMaster.dta" 
drop _merge


*** demographics ***
recode gender 2=1 else=0,gen(female)
label variable female
recode race 2=1 else=0,gen(black)
label variable black
recode race 3=1 else=0,gen(latino)
label variable latino
recode race 4/8 =1 else=0,gen(othrace)
label variable othrace
recode faminc 31 32 97=.
recode faminc .=1 else=0,gen(famincna)
label variable famincna
sum faminc ,d
replace faminc=`r(p50)' if faminc==.


*** ideology ***
recode cc334a 8=.,gen(respideo)
label values respideo LABN
label variable respideo

gen demideo=cc334p if cc334p>=1 & cc334p<=7
replace demideo=. if sencand1party~="Democratic"  // drops orman //
label values demideo LABN
label variable demideo

gen repideo=cc334q if cc334q>=1 & cc334q<=7
replace repideo=. if sencand2party~="Republican"
label values repideo LABN
label variable repideo


*** PID ***
drop pid3
recode pid7 (1 2 3=0 "Dem") (5 6 7=2 "Rep") (else=1 "Ind/DK"),gen(pid3)
label variable pid3


*** vote choice ***
recode cc410b (1=0 "Dem")(2=1 "Rep")(else=.),gen(vote)
replace vote=. if sencand1party~="Democratic"  // drops orman & AL, with Sessions uncontested //
replace vote=. if sencand2party~="Republican"
label variable vote

*** Incumbency ***
gen incumb=0
replace incumb=1 if sencand2incumbent=="1"
replace incumb=-1 if sencand1incumbent=="1"
replace incumb=. if sencand1party~="Democratic"  // drops orman & AL, with Sessions uncontested //
replace incumb=. if sencand2party~="Republican"

gen year=2014
keep weight abbrev faminc female-year state-region
compress
save temp1.dta ,replace


*** computing mean positions ***
clear
use temp1.dta
collapse demideo repideo [pw=weight] ,by(abbrev)
drop if demideo==.|repideo==.
gen demideomean=round(demideo,.001)
gen repideomean=round(repideo,.001)
keep abbrev demideomean repideomean
save temp2.dta ,replace


*** merging ***
clear
use temp1.dta
merge m:1 abbrev using temp2.dta

compress
save cces2014_analysis.dta ,replace
erase temp1.dta
erase temp2.dta
